<?php

namespace common\helpers;

use Yii;

/**
 * 格式化地址处理器
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class Url extends \yii\helpers\BaseUrl {

    /**
     * 获取H5页面地址
     * @param string $uri
     * @return string
     */
    public static function h5($uri) {
        $domain = ArrayHelper::getValue(Yii::$app->params, 'domain.h5');
        return static::toRemote(static::to($uri), $domain);
    }

    /**
     * 获取分享地址
     * @param string $uri
     * @return string
     */
    public static function share($uri) {
        $domain = ArrayHelper::getValue(Yii::$app->params, 'domain.share');
        return static::toRemote(static::to($uri), $domain);
    }

    /**
     * 获取视频地址
     * @param string $original
     * @param string|null $host
     * @return string
     */
    public static function video($original, $host = null) {
        return static::toRemote($original, $host ? $host : Yii::getAlias('@attachUrl'));
    }

    /**
     * 获取远程附件地址
     * @param string $original
     * @param string|null $host
     * @return string
     */
    public static function attach($original, $host = null) {
        return static::toRemote($original, $host ? $host : Yii::getAlias('@attachUrl'));
    }

    /**
     * 获取支付回调地址
     * @param string $paymode
     * @return string
     */
    public static function callbackPayment($paymode) {
        $url = ArrayHelper::getValue(Yii::$app->params, 'domain.paymentNotifyUrl');
        return static::toRemote($paymode, $url);
    }

    /**
     * 格式化内容成可访问的Url地址
     * @param string $original
     * @param string|null $host
     * @return string
     */
    public static function toRemote($original, $host = null) {
        if (!$original) {
            return '';
        }
        if (!static::isRelative($original)) {
            return $original;
        }
        if ($host && !static::isRelative($host)) {
            return rtrim($host, '/') . '/' . ltrim($original, '/');
        }
        return $original;
    }

    /**
     * 生成短网址
     * @param string $url
     * @return string
     */
    public static function toShort($url) {
        if (is_array($url)) {
            $url = static::to($url);
        }
        $result = sprintf("%u", crc32($url));
        $sUrl = '';
        while ($result > 0) {
            $s = $result % 62;
            if ($s > 35) {
                $s = chr($s + 61);
            } elseif ($s > 9 && $s <= 35) {
                $s = chr($s + 55);
            }
            $sUrl .= $s;
            $result = floor($result / 62);
        }
        return $sUrl;
    }

    public static function shorturl($url) {
        $input = static::to($url);
        $base32 = [
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
            'y', 'z', '0', '1', '2', '3', '4', '5'
        ];
        $hex = md5($input);
        $hexLen = strlen($hex);
        $subHexLen = $hexLen / 8;
        $output = array();
        for ($i = 0; $i < $subHexLen; $i++) {
            //把加密字符按照8位一组16进制与0x3FFFFFFF(30位1)进行位与运算
            $subHex = substr($hex, $i * 8, 8);
            $int = 0x3FFFFFFF & (1 * filter_var("0x" . $subHex, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX));
            $out = '';
            for ($j = 0; $j < 6; $j++) {
                //把得到的值与0x0000001F进行位与运算，取得字符数组chars索引
                $val = 0x0000001F & $int;
                $out .= $base32[$val];
                $int = $int >> 5;
            }
            $output[] = $out;
        }

        return $output;
    }

}
